﻿using Dapper;
using IRealize;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Realize
{
    public class Config_Major_Zw:IConfig_Major_Zw
    {
        public async Task<int> Add(string name, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $"insert into config_major_zw(major_name) values('{name}')";
                return await con.ExecuteAsync(sql);
            }
        }

        public async Task<int> Delete(string id, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $"delete from config_major_zw where major_zwid='{id}'";
                return await con.ExecuteAsync(sql);
            }
        }

        public async Task<List<Entity.Config_Major_Zw>> select(string sqlserver)
        {
            Task<List<Entity.Config_Major_Zw>> task = Task<List<Entity.Config_Major_Zw>>.Run(async () =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {
                    string sql = "select * from config_major_zw";
                    return con.Query<Entity.Config_Major_Zw>(sql).ToList();
                }

            });
            return await task;
        }

        public async Task<int> Upd(string id, string name, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $"update config_major_zw set major_name='{name}' where major_zwid={id}";
                return await con.ExecuteAsync(sql);
            }
        }

        public async Task<Entity.Config_Major_Zw> GetById(string id, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = "select * from config_major_zw where major_zwid = @Id";
                return await con.QueryFirstOrDefaultAsync<Entity.Config_Major_Zw>(sql, new { Id = id });
            }
        }
    }
}
